package cn.itcast.travel.service.impl;

import cn.itcast.travel.service.SpeechService;
import cn.itcast.travel.util.JedisUtil;
import cn.itcast.travel.util2.CityUtil;
import redis.clients.jedis.Jedis;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class SpeechServiceImp implements SpeechService {

    @Override
    public String find(String s) throws IOException {
        Jedis jedis = JedisUtil.getJedis();
        List<String> allCity = jedis.lrange("allCity", 0, -1);
        if (allCity==null||allCity.size()==0){
            InputStream resourceAsStream = this.getClass().getClassLoader().getResourceAsStream("city.txt");
            Reader reader=new InputStreamReader(resourceAsStream);
            BufferedReader bufferedReader=new BufferedReader(reader);
            String s5="";
            String a;
            while ((a=bufferedReader.readLine())!=null){
                s+=a;
            }
            String[] split = s.split("、");
            List<String> list=new ArrayList<>(999);
            for (String s1:split){
                list.add(s1);
                jedis.lpush("allCity",s1);
            }
            allCity=list;
            String r = CityUtil.getR(s, allCity);
            return r;
        }else {
         String   r=CityUtil.getR(s,allCity);
            return r;
        }

    }
}
